//
//  SkinUpdateInterface.m
//  CPICApp
//
//  Created by zhiwei li on 13-3-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "IFServiceEvaluation.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "ViewController.h"
#import "AppDelegate.h"
#import "AddServiceEvaluationViewController.h"

@implementation IFServiceEvaluation
@synthesize progress;
@synthesize ServiceModuleName;
@synthesize ServiceModuleID;

-(void)setIfServiceEvaluationRequest//向太保移动平台发送服务模块是否需要服务评价接口请求报文
{
    NSDictionary *dictUserInfo=[[NSUserDefaults standardUserDefaults] dictionaryForKey:DICT_USER_INFO];
    NSString * UserAccout;
    if (dictUserInfo!=nil) 
    {
        if ([dictUserInfo valueForKey:@"UserAccount"])
        {
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserAccount"]);
            UserAccout = [dictUserInfo valueForKey:@"UserAccount"];
            if ([UserAccout isEqualToString:@""]) {
                [[AppDelegate app].unc popViewControllerAnimated:YES];
                return;
            }
        }
    }
    else 
    {
       UserAccout = @"";
       //[[AppDelegate app].unc popViewControllerAnimated:YES];
       return;
    }
        
//    NSString *strRequestCode=@"RequestCode=31601";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    [dicRequest setObject:UserAccout forKey:@"userAccount"];
    [dicRequest setObject:self.ServiceModuleID forKey:@"ServiceModuleID"];
    [dicRequest setObject:self.ServiceModuleName forKey:@"ServiceModuleName"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
    requestEvaluate= [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [requestEvaluate setPostValue:@"31601" forKey:@"RequestCode"];
    [requestEvaluate setPostValue:strRequestJson forKey:@"RequestData"];
    
    [requestEvaluate setDelegate:self];
    [requestEvaluate setDidFailSelector:@selector(ResponseFail:)];
    [requestEvaluate setDidFinishSelector:@selector(ResonseSuccess:)];
    [requestEvaluate startAsynchronous];
    
    showProgress = YES;
    if (showProgress) 
    {
        progress= [[MBProgressHUD alloc]initWithView:self];
        [self addSubview:progress];
        [self bringSubviewToFront:progress];
        progress.labelText = @"加载中...";
        [progress show:NO];
        showProgress = NO;
        [progress setHidden:YES];
    };
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    
    NSMutableDictionary *jsonoSubObj = [dictResponse objectForKey:@"head"];
    NSString * ss = [[NSString alloc] initWithFormat:@"%@",[jsonoSubObj objectForKey:@"success"]];
    NSString * strEvaluate = [[NSString alloc] initWithFormat:@"%@",[dictResponse objectForKey:@"evaluate"]];
    
    if ([strEvaluate isEqualToString:@"1"])
    {
        NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
        [myDefaults setObject:strEvaluate forKey:self.ServiceModuleID];
    }
    else
    {
        NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
        [myDefaults setObject:@"0" forKey:self.ServiceModuleID];
    }

//    if ([ss isEqualToString:@"1"])
//    {
//        if ([strEvaluate isEqualToString:@"1"]) {
////            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未评论，需要评价！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消",nil];
////            [alertView show];
////            alertView.delegate = self;
////            [alertView release];
//            AddServiceEvaluationViewController *serviceevluation = [[AddServiceEvaluationViewController alloc]initView:self.ServiceModuleID withserviceName:self.ServiceModuleName];
//            [[AppDelegate app].unc pushViewController:serviceevluation animated:YES];
//            [serviceevluation release];
//        }
//        else
//        {
//            [[AppDelegate app].unc popViewControllerAnimated:YES];
//        }
//    }
//    else
//    {
//        return;
//    }
}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    //[self justShowMessage:@"服务器连接失败" title:@"注意"];
    //[[AppDelegate app].unc popViewControllerAnimated:YES];
}

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(alert.tag)
    {
        case 0:
            if (buttonIndex == [alert cancelButtonIndex])
                //默认cancelButtonIndex = 0，每个按钮index可设
            {
                AddServiceEvaluationViewController *serviceevluation = [[AddServiceEvaluationViewController alloc]initView:self.ServiceModuleID withserviceName:self.ServiceModuleName];
                [[AppDelegate app].unc pushViewController:serviceevluation animated:YES];
                [serviceevluation release];
            }
            else 
            {
                [[AppDelegate app].unc popViewControllerAnimated:YES];
            }
            break;
        case 1:
            return;
        default:
            break;
    }
}

-(id) initView:(NSString *)serviceID withserviceName:(NSString *)serviceName
{
    self.ServiceModuleID = serviceID;
    self.ServiceModuleName = serviceName;
    self = [super init];
    return self;
}

-(void) dealloc
{
    if (requestEvaluate) {
        [requestEvaluate clearDelegatesAndCancel];
        requestEvaluate=nil;
    }
    [ServiceModuleName release];
    [ServiceModuleID release];
    [progress release];
    [super dealloc];
}

@end
